package com.lawliet.example.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawliet.example.model.system.SysDictItem;
import com.lawliet.example.model.vo.Result;
import com.lawliet.example.service.system.SysDictItemService;
import com.lawliet.example.service.system.SysDictService;
import com.lawliet.example.util.QueryHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Tag(name = "数据字典项接口")
@RestController
@RequestMapping("/api/sysDictItem")
public class SysDictItemController {

    @Autowired
    private SysDictItemService sysDictItemService;

    @Autowired
    private SysDictService sysDictService;

    @Operation(summary = "数据字典项查询")
    @GetMapping("/list")
    public Result<?> list(@Parameter(description = "查询条件") SysDictItem data,
                          @Parameter(description = "当前页") @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @Parameter(description = "每页显示条数") @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                          HttpServletRequest req) {

        Page<SysDictItem> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SysDictItem> sysUserQueryWrapper = QueryHelper.buildQueryWrapper(data);
        sysUserQueryWrapper.eq("DEL_FLAG",0);//没有被删除的
        IPage<SysDictItem> pageList = sysDictItemService.page(page, sysUserQueryWrapper);

        return Result.OK(pageList);
    }

    @Operation(summary = "数据字典项新增")
    @PostMapping("/add")
    public Result<?> add(@RequestBody SysDictItem sysRole) {
        sysDictItemService.save(sysRole);
        try {
            sysDictService.loadDictToRedis();
        }catch (Exception e){
            System.out.println("封装字典数据数据时报错："+e.getMessage());
        }
        return Result.OK("修改成功！！！");
    }

    @Operation(summary = "数据字典项编辑")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody SysDictItem sysRole) {
        sysDictItemService.updateById(sysRole);
        try {
            sysDictService.loadDictToRedis();
        }catch (Exception e){
            System.out.println("封装字典数据数据时报错："+e.getMessage());
        }
        return Result.OK("编辑成功！！！");
    }

    @Operation(summary = "数据字典项删除")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String ids) {
        sysDictItemService.removeByIds(Arrays.asList(ids.split(",")));
        try {
            sysDictService.loadDictToRedis();
        }catch (Exception e){
            System.out.println("封装字典数据数据时报错："+e.getMessage());
        }
        return Result.OK("删除成功！！！");
    }

    @GetMapping("/query/{id}")
    public Result<SysDictItem> queryById(@PathVariable Long id) {
        return Result.OK(sysDictItemService.getById(id));
    }
}
